package com.java.mybatis.controller;

import com.java.mybatis.entity.*;
import com.java.mybatis.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * (Outbound)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/outbound/")
public class OutboundController {
    /**
     * 服务对象
     */
    @Resource
    private OutboundService outboundService;

    @Autowired
    private LeavewarehouseService leavewarehouseService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private LogisticsService logisticsService;

    @Autowired
    private CarService carService;


    /**
     * 分页查询
     * @param request
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("queryOutboundLimit")
    public String queryOutboundLimit(HttpServletRequest request, Model model, Integer page){
        request.getSession().removeAttribute("outboundLikeName");
        //调用方法  查询数据
        List<Outbound> outboundList = outboundService.queryOutboundLimit(0, (page - 1) * 7, 7);
        //存入数据
        model.addAttribute("outbound",outboundList);
        //存入当前页
        request.getSession().setAttribute("thisPage",page);
        //总数据条数
        int count = outboundService.queryCount(0);
        //存入总数据
        request.getSession().setAttribute("count",count);
        //总共有多少页
        int goodPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
        model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
        //存入总共有多少页
        model.addAttribute("goodPageMax",goodPageMax);
        //存入总共多少条数据
        model.addAttribute("count",count);
        return "System/sys-qq";
    }

    @RequestMapping("queryStateLike")
    public String queryStateLike(HttpServletRequest request,Model model,Integer page,String outboundNumber){
        if (outboundNumber!=null&&outboundNumber!="") {
            request.getSession().setAttribute("outboundLikeName",outboundNumber);
            HashMap<String, Object> map = new HashMap<>();
            map.put("str",outboundNumber);
            map.put("page",(page-1)*7);
            map.put("pageSize",7);

            List<Outbound> outbounds = outboundService.queryStateLikeOutboundNumber(map);
            //存入数据
            model.addAttribute("outbound",outbounds);
            //存入当前页
            request.getSession().setAttribute("thisPage",page);
            //总条数
            int count = outboundService.queryStateLikeCount("%" + outboundNumber + "%");
            int goodPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
            model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
            //存入总共有多少页
            model.addAttribute("goodPageMax",goodPageMax);
            //存入总共多少条数据
            model.addAttribute("count",count);
            return "System/sys-qq";
        }else{
            return "redirect:/outbound/queryOutboundLimit?page=1";
        }
    }

    /**
     * 出库复核
     * @param session
     * @param outboundId
     * @return
     */
    @RequestMapping("updateOutboundState")
    public String updateOutboundState(HttpSession session,Integer car, Integer outboundId,String outboundNumber,String outboundMark){
        Integer thisPage = aa(session,car, outboundId, outboundNumber,outboundMark);
        return "redirect:/outbound/queryOutboundLimit?page="+thisPage;
    }
    private Integer aa(HttpSession session,Integer car,Integer outboundId,String outboundNumber,String outboundMark){
        //出库复核
        int i = outboundService.updateOutboundState(outboundId);
        Integer thisPage = (Integer) session.getAttribute("thisPage");

        //添加出库记录
        //设置对象   添加的数据
        Leavewarehouse leavewarehouse = new Leavewarehouse();
        //出库编号
        leavewarehouse.setLeavewarehouseSerial(outboundNumber);
        //设置出库批号id
        Outbound outbound = new Outbound();
        outbound.setOutboundId(outboundId);
        leavewarehouse.setOutbound(outbound);
        //设置出库数量
        int sumGoodsNumber = goodsService.selectGoodsNumber(outboundNumber);;
        leavewarehouse.setLeavewarehouseNumber(sumGoodsNumber);
        //设置出库负责人   当前登录人
        Employees emp = (Employees) session.getAttribute("emp");
        System.out.println("--------------------------"+emp);
        leavewarehouse.setEmployees(emp);
        //设置出库时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        System.out.println("----------------------======"+format);
        leavewarehouse.setLeavewarehouseTime(format);
        //设置出库地区  发货地区
        leavewarehouse.setLeavewarehouseAddress(outboundMark);
        //设置出库状态
        leavewarehouse.setLeavewarehouseState(1);
        //设置软删除状态
        leavewarehouse.setLeavewarehouseCode(1);
        //设置备用字段
        leavewarehouse.setLeavewarehouseMark("备用字段");
        //添加
        leavewarehouseService.insertLeavewarehouse(leavewarehouse);

        //查询leavewarehouse表的最大id   等同于最后添加的id
        int queryByIdMax = leavewarehouseService.queryByIdMax();
        //查询logistics最大id   用于添加数据
        int queryByIdMaxLogistics = logisticsService.queryByIdMaxLogistics();
        Logistics logistics = new Logistics();
        logistics.setLogisticsNumber("#lk150"+(queryByIdMaxLogistics+1));
        Car car1 = new Car();
        car1.setCarId(car);
        logistics.setCar(car1);
        Leavewarehouse leavewarehouse1 = new Leavewarehouse();
        leavewarehouse1.setLeavewarehouseId(queryByIdMax);
        logistics.setLeavewarehouse(leavewarehouse1);
        logistics.setLogisticsOuttime(format);
        logistics.setLogisticsState(0);
        logistics.setLogisticsCode(1);
        logistics.setLogisticsMark("备用");
        logisticsService.addLogistics(logistics);

        carService.updateByIdState(car);


        return thisPage;
    }

    /**
     * 出库批量复核
     * @param session
     * @param str
     * @return
     */
    @RequestMapping("updateOutboundStateAll")
    public String updateOutboundStateAll(HttpSession session,String str,String outboundNumber){
        System.out.println(str+"-----------------------");
        Integer thisPage=0;
        for (String s : str.split(",")) {
            //thisPage= aa(session, Integer.parseInt(s), outboundNumber);
        }
        return "redirect:/outbound/queryOutboundLimit?page="+thisPage;
    }
}